package fr.inserm.u915.tabixserver;

import java.io.IOException;
import java.io.OutputStream;

import javax.xml.stream.XMLStreamException;

public class XmlHandler
	extends AbstractXmlHandler
	{
	String xsduri=null;
	
	public XmlHandler(OutputStream out, String encoding)
			throws IOException
		{
		super(out, encoding);
		}

	@Override
	public void writeStartDocument() throws IOException
		{
		super.writeStartDocument();
		
		try
			{
			getWriter().writeStartElement("tabix");
			getWriter().setDefaultNamespace(TNS.NS);
			if(xsduri!=null)
				{
				getWriter().writeAttribute("xmlns:"+XSI.prefix,XSI.NS);
				getWriter().writeAttribute("xmlns",TNS.NS);
				getWriter().writeAttribute(XSI.prefix,XSI.NS,"schemaLocation",xsduri);
				}
			}
		catch (XMLStreamException e)
			{
			throw new IOException(e);
			}
		}
	
	@Override
	public void writeEndDocument() throws IOException
		{
		try
			{
			getWriter().writeEndElement();
			}
		catch (XMLStreamException e)
			{
			throw new IOException(e);
			}
		super.writeEndDocument();
		}	
	
	@Override
	public void writeStartQuery(Segment segment) throws IOException
		{
		super.writeStartQuery(segment);
		try
			{
			getWriter().writeStartElement("query");
			getWriter().writeAttribute("chrom", segment.getChromosome());
			getWriter().writeAttribute("start", String.valueOf(segment.getStart()));
			getWriter().writeAttribute("end", String.valueOf(segment.getEnd()));
			}
		catch (XMLStreamException e)
			{
			throw new IOException(e);
			}

		}

	@Override
	public void writeEndQuery() throws IOException
		{
		try
			{
			getWriter().writeEndElement();
			}
		catch (XMLStreamException e)
			{
			throw new IOException(e);
			}
		super.writeEndQuery();
		}
	
	@Override
	public void writeStartInstance(Instance instance) throws IOException
		{
		super.writeStartInstance(instance);
		try
			{
			getWriter().writeStartElement("instance");
			getWriter().writeAttribute("id", instance.getId());
			getWriter().writeAttribute("label", instance.getLabel());
			getWriter().writeAttribute("description",  instance.getDescription());
			getWriter().writeAttribute("build", instance.getBuild().getId());
			}
		catch (XMLStreamException e)
			{
			throw new IOException(e);
			}

		}
	
	@Override
	public void writeEndInstance() throws IOException
		{
		try
			{
			getWriter().writeEndElement();
			}
		catch (XMLStreamException e)
			{
			throw new IOException(e);
			}
		super.writeEndInstance();
		}

	@Override
	public int writeRow(String[] tokens) throws IOException
		{
		try
			{
			getWriter().writeStartElement(getInstance().getId());
			getWriter().writeAttribute("index", String.valueOf(getRowCount()));
			for(int i=0;i< getInstance().size();++i)
				{
				Column c=getInstance().get(i);
				if(c.isIgnore()) continue;
				getWriter().writeStartElement(c.getId());
				getWriter().writeCharacters(tokens[c.getColumnIndex()]);
				getWriter().writeEndElement();
				}
			getWriter().writeEndElement();
			}
		catch (XMLStreamException e)
			{
			throw new IOException(e);
			}
		return _next();
		}
	
	}
